package com.htc.webservice;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.graphics.drawable.Drawable.ConstantState;

import com.htc.constants.Constants;
import com.htc.model.dto.AboutsDTO;
import com.htc.model.dto.ChangeLogDTO;
import com.htc.model.dto.EventsDTO;
import com.htc.model.dto.FitnessClassDTO;
import com.htc.model.dto.FitnessGroupDTO;
import com.htc.model.dto.FitnessScheduleDTO;
import com.htc.model.dto.InstructorDTO;
import com.htc.model.dto.MerchantDTO;
import com.htc.model.dto.NewsModuleDTO;
import com.htc.model.dto.RetailersDTO;
import com.htc.model.dto.SpaChildDTO;
import com.htc.model.dto.SpaParentDTO;
import com.htc.model.utils.AboutModel;
import com.htc.model.utils.EventsModel;
import com.htc.model.utils.FitnessClassModel;
import com.htc.model.utils.FitnessGroupModel;
import com.htc.model.utils.FitnessScheduleModel;
import com.htc.model.utils.InstructorModel;
import com.htc.model.utils.MerchantModel;
import com.htc.model.utils.NewsModel;
import com.htc.model.utils.RetailerModel;
import com.htc.model.utils.SpaChildModel;
import com.htc.model.utils.SpaParentModel;

public class ChangeLogService {
	private static ChangeLogService instance;

	private static String TAG_ID = "change_log_id";
	private static String TAG_CHANGE_LOG_TBL_NAME = "change_log_table_name";
	private static String TAG_CHANGE_LOG_TBL_PK = "change_log_table_pk";
	private static String TAG_CHANGE_LOG_TBL_ACTION = "change_log_table_action";
	private static String TAG_CHANGE_LOG_TBL_CONTENT = "change_log_table_content";
	private static String TAG_ADDED_DATE = "added_date";

	private ArrayList<ChangeLogDTO> arrChangeLog;

	public static ChangeLogService getInstance() {
		if (instance == null) {
			instance = new ChangeLogService();
		}

		return instance;
	}

	private int getTableName(String tableName) {
		int tableAction = 0;
		if (tableName.compareTo(Constants.TBL_ABOUT_US) == 0) {
			tableAction = Constants.ID_ABOUT_US;
		} else if (tableName.compareTo(Constants.TBL_CHANGE_LOG) == 0) {
			tableAction = Constants.ID_CHANGE_LOG;
		} else if (tableName.compareTo(Constants.TBL_EVENT_MODULE) == 0) {
			tableAction = Constants.ID_EVENT_MODULE;
		} else if (tableName.compareTo(Constants.TBL_FITNESS_CLASS) == 0) {
			tableAction = Constants.ID_FITNESS_CLASS;
		} else if (tableName.compareTo(Constants.TBL_FITNESS_GROUP) == 0) {
			tableAction = Constants.ID_FITNESS_GROUP;
		} else if (tableName.compareTo(Constants.TBL_FITNESS_SCHEDULE) == 0) {
			tableAction = Constants.ID_FITNESS_SCHEDULE;
		} else if (tableName.compareTo(Constants.TBL_INSTRUCTOR) == 0) {
			tableAction = Constants.ID_INSTRUCTOR;
		} else if (tableName.compareTo(Constants.TBL_MERCHANT) == 0) {
			tableAction = Constants.ID_MERCHANT;
		} else if (tableName.compareTo(Constants.TBL_MERCHANT_BRANCHES) == 0) {
			tableAction = Constants.ID_MERCHANT_BRANCHES;
		} else if (tableName.compareTo(Constants.TBL_NEWS_MODULE) == 0) {
			tableAction = Constants.ID_NEWS_MODULE;
		} else if (tableName.compareTo(Constants.TBL_RETAILER) == 0) {
			tableAction = Constants.ID_RETAILER;
		} else if (tableName.compareTo(Constants.TBL_SPA_CATEGORY) == 0) {
			tableAction = Constants.ID_SPA_CATEGORY;
		} else if (tableName.compareTo(Constants.TBL_SPA_SECTION) == 0) {
			tableAction = Constants.ID_SPA_SECTION;
		}

		return tableAction;
	}

	private int getActionProcessData(String strAction) {
		int action = 0;

		if (strAction.compareTo(Constants.STR_ACTION_INSERT) == 0) {
			action = Constants.ACTION_INSERT;
		} else if (strAction.compareTo(Constants.STR_ACTION_UPDATE) == 0) {
			action = Constants.ACTION_UPDATE;
		} else if (strAction.compareTo(Constants.STR_ACTION_DELETE) == 0) {
			action = Constants.ACTION_DELETE;
		}

		return action;
	}

	public int synchronizeDatabase(String json, Context context)
			throws JSONException, Exception {
		int tag = 1;

		ArrayList<ChangeLogDTO> arr = parseChangeLogTable(json);
		if (arr.size() > 0) {
			for (ChangeLogDTO dto : arr) {
				if (dto.changeLogTblName.compareTo(Constants.TBL_ABOUT_US) == 0) {
					AboutsDTO object = new AboutsDTO();
					object.set_id(dto.changeLogTblPk);
					int action = getActionProcessData(dto.changeLogTblAction);
					if (action != Constants.ACTION_DELETE) {
						object = parseAboutFromJson(dto.changeLogTblContent,
								dto.changeLogTblPk);
					}
					AboutModel about = AboutModel.getInstance();
					about.insertAbout(object, context);
					// TODO: insert/update/delete to database here
					AboutModel aboutus = AboutModel.getInstance();
					switch (action) {
					case Constants.ACTION_INSERT:
						aboutus.insertAbout(object, context);
						break;
					case Constants.ACTION_UPDATE:
						aboutus.updateAbout(object, context);
						break;

					case Constants.ACTION_DELETE:
						aboutus.deleteAbout(object, context);
						break;
					default:
						break;
					}
				} else if (dto.changeLogTblName
						.compareTo(Constants.TBL_EVENT_MODULE) == 0) {
					EventsDTO object = new EventsDTO();
					object.set_id(dto.changeLogTblPk);
					int action = getActionProcessData(dto.changeLogTblAction);
					if (action != Constants.ACTION_DELETE) {
						object = parseEventsFromJson(dto.changeLogTblContent,
								dto.changeLogTblPk);
					}
					// TODO: insert/update/delete to database here
					EventsModel event = EventsModel.getInstance();
					switch (action) {
					case Constants.ACTION_INSERT:
						event.insertEvent(object, context);
						break;
					case Constants.ACTION_UPDATE:
						event.updateEvents(object, context);
						break;
					case Constants.ACTION_DELETE:
						event.deleteEvents(object, context);
						break;

					default:
						break;
					}

				} else if (dto.changeLogTblName
						.compareTo(Constants.TBL_FITNESS_CLASS) == 0) {
					FitnessClassDTO object = new FitnessClassDTO();
					object.setFitnessClassId(dto.changeLogTblPk);
					int action = getActionProcessData(dto.changeLogTblAction);
					if (action != Constants.ACTION_DELETE) {
						object = parseFitnessClassFromJson(
								dto.changeLogTblContent, dto.changeLogTblPk);
					}
					// TODO: insert/update/delete to database here
					FitnessClassModel fitclass = FitnessClassModel
							.getInstance();
					switch (action) {
					case Constants.ACTION_INSERT:
						fitclass.insertFitClass(object, context);
						break;
					case Constants.ACTION_UPDATE:
						fitclass.updateFitClass(object, context);
						break;

					case Constants.ACTION_DELETE:
						fitclass.deleteFitClass(object, context);
						break;
					default:
						break;
					}
				} else if (dto.changeLogTblName
						.compareTo(Constants.TBL_FITNESS_GROUP) == 0) {
					FitnessGroupDTO object = new FitnessGroupDTO();
					object.setFitnessGroupId(dto.changeLogTblPk);
					int action = getActionProcessData(dto.changeLogTblAction);
					if (action != Constants.ACTION_DELETE) {
						object = parseFitnessGroupFromJson(
								dto.changeLogTblContent, dto.changeLogTblPk);
					}
					// TODO: insert/update/delete to database here
					FitnessGroupModel fitgroup = FitnessGroupModel
							.getInstance();
					switch (action) {
					case Constants.ACTION_INSERT:
						fitgroup.insertFitGroup(object, context);
						break;
					case Constants.ACTION_UPDATE:
						fitgroup.updateFitGroup(object, context);
						break;

					case Constants.ACTION_DELETE:
						fitgroup.deleteFitGroup(object, context);
						break;
					default:
						break;
					}
				} else if (dto.changeLogTblName
						.compareTo(Constants.TBL_FITNESS_SCHEDULE) == 0) {
					FitnessScheduleDTO object = new FitnessScheduleDTO();
					object.setFitnessScheduleId(dto.changeLogTblPk);
					int action = getActionProcessData(dto.changeLogTblAction);
					if (action != Constants.ACTION_DELETE) {
						object = parseFitnessScheduleFromJson(
								dto.changeLogTblContent, dto.changeLogTblPk);
					}
					// TODO: insert/update/delete to database here
					FitnessScheduleModel fitche = FitnessScheduleModel
							.getInstance();
					switch (action) {
					case Constants.ACTION_INSERT:
						fitche.insertFitSche(object, context);
						break;
					case Constants.ACTION_UPDATE:
						fitche.updateFitSche(object, context);
						break;

					case Constants.ACTION_DELETE:
						fitche.deleteFitSche(object, context);
						break;
					default:
						break;
					}
				} else if (dto.changeLogTblName
						.compareTo(Constants.TBL_INSTRUCTOR) == 0) {
					InstructorDTO object = new InstructorDTO();
					object.setInstrutorId(dto.changeLogTblPk);
					int action = getActionProcessData(dto.changeLogTblAction);
					if (action != Constants.ACTION_DELETE) {
						object = parseInstructorFromJson(
								dto.changeLogTblContent, dto.changeLogTblPk);
					}
					InstructorModel instruc = InstructorModel.getInstance();
					switch (action) {
					case Constants.ACTION_INSERT:
						instruc.insertInstruc(object, context);
						break;
					case Constants.ACTION_UPDATE:
						instruc.updateInstruc(object, context);
						break;

					case Constants.ACTION_DELETE:
						instruc.deleteInstruc(object, context);
						break;
					default:
						break;
					}

				} else if (dto.changeLogTblName
						.compareTo(Constants.TBL_MERCHANT) == 0) {
					MerchantDTO object = new MerchantDTO();
					object.setMerchantId(dto.changeLogTblPk);
					int action = getActionProcessData(dto.changeLogTblAction);
					if (action != Constants.ACTION_DELETE) {
						object = parseMerchantFromJson(dto.changeLogTblContent,
								dto.changeLogTblPk);
					}
					// TODO: insert/update/delete to database here
					MerchantModel merchan = MerchantModel.getInstance();
					switch (action) {
					case Constants.ACTION_INSERT:
						merchan.insertMerchan(object, context);
						break;
					case Constants.ACTION_UPDATE:
						merchan.updateMerchan(object, context);
						break;

					case Constants.ACTION_DELETE:
						merchan.deleteMerchan(object, context);
						break;
					default:
						break;
					}
				} else if (dto.changeLogTblName
						.compareTo(Constants.TBL_MERCHANT_BRANCHES) == 0) {
					MerchantDTO object = new MerchantDTO();
					object.setMerchantId(dto.changeLogTblPk);
					int action = getActionProcessData(dto.changeLogTblAction);
					if (action != Constants.ACTION_DELETE) {
						object = parseMerchantFromJson(dto.changeLogTblContent,
								dto.changeLogTblPk);
					}
					// TODO: insert/update/delete to database here
					switch (action) {
					case Constants.ACTION_INSERT:

						break;
					case Constants.ACTION_UPDATE:

						break;

					case Constants.ACTION_DELETE:

						break;
					default:
						break;
					}
				} else if (dto.changeLogTblName
						.compareTo(Constants.TBL_NEWS_MODULE) == 0) {
					NewsModuleDTO object = new NewsModuleDTO();
					object.setNewsModuleId(dto.changeLogTblPk);
					int action = getActionProcessData(dto.changeLogTblAction);
					if (action != Constants.ACTION_DELETE) {
						object = parseNewsModuleFromJson(
								dto.changeLogTblContent, dto.changeLogTblPk);
					}
					// TODO: insert/update/delete to database here
					NewsModel news = NewsModel.getInstance();
					switch (action) {
					case Constants.ACTION_INSERT:
						news.insertNews(object, context);
						break;
					case Constants.ACTION_UPDATE:
						news.updateNews(object, context);
						break;

					case Constants.ACTION_DELETE:
						news.deleteNews(object, context);
						break;
					default:
						break;
					}
				} else if (dto.changeLogTblName
						.compareTo(Constants.TBL_RETAILER) == 0) {
					RetailersDTO object = new RetailersDTO();
					object.setRetailerId(dto.changeLogTblPk);
					int action = getActionProcessData(dto.changeLogTblAction);
					if (action != Constants.ACTION_DELETE) {
						object = parseRetailerFromJson(dto.changeLogTblContent,
								dto.changeLogTblPk);
					}
					RetailerModel retailer = RetailerModel.getInstance();
					// TODO: insert/update/delete to database here
					switch (action) {
					case Constants.ACTION_INSERT:
						retailer.insertRetailer(object, context);
						break;
					case Constants.ACTION_UPDATE:
						retailer.updateRetailer(object, context);
						break;

					case Constants.ACTION_DELETE:
						retailer.deleteRetailer(object, context);
						break;
					default:
						break;
					}
				} else if (dto.changeLogTblName
						.compareTo(Constants.TBL_SPA_CATEGORY) == 0) {
					SpaParentDTO object = new SpaParentDTO();
					object.set_id(dto.changeLogTblPk);
					int action = getActionProcessData(dto.changeLogTblAction);
					if (action != Constants.ACTION_DELETE) {
						object = parseSpaParentFromJson(
								dto.changeLogTblContent, dto.changeLogTblPk);
					}
					// TODO: insert/update/delete to database here
					SpaParentModel spaParent = SpaParentModel.getInstance();
					switch (action) {
					case Constants.ACTION_INSERT:
						spaParent.insertSpapa(object, context);
						break;
					case Constants.ACTION_UPDATE:
						spaParent.updateSpapa(object, context);
						break;

					case Constants.ACTION_DELETE:
						spaParent.deleteSpapa(object, context);
						break;
					default:
						break;
					}
				} else if (dto.changeLogTblName
						.compareTo(Constants.TBL_SPA_SECTION) == 0) {
					SpaChildDTO object = new SpaChildDTO();
					object.setSecId(dto.changeLogTblPk);
					int action = getActionProcessData(dto.changeLogTblAction);
					if (action != Constants.ACTION_DELETE) {
						object = parseSpaChildFromJson(dto.changeLogTblContent,
								dto.changeLogTblPk);
					}
					// TODO: insert/update/delete to database here
					SpaChildModel spaChilderd = SpaChildModel.getInstance();
					switch (action) {
					case Constants.ACTION_INSERT:
						spaChilderd.insertSpachil(object, context);
						break;
					case Constants.ACTION_UPDATE:
						spaChilderd.updateSpachil(object, context);
						break;

					case Constants.ACTION_DELETE:
						spaChilderd.deleteSpachil(object, context);
						break;
					default:
						break;
					}
				} else {
					tag = -1;
				}
			}
		}

		return tag;
	}

	public ArrayList<ChangeLogDTO> parseChangeLogTable(String json)
			throws JSONException {
		arrChangeLog = new ArrayList<ChangeLogDTO>();

		JSONArray jsonArray = new JSONArray(json);
		for (int i = 0; i < jsonArray.length(); i++) {
			JSONObject object = jsonArray.getJSONObject(i);
			ChangeLogDTO dto = new ChangeLogDTO();
			if (object.has(TAG_ID))
				dto.id = object.getString(TAG_ID);
			if (object.has(TAG_CHANGE_LOG_TBL_NAME))
				dto.changeLogTblName = object
						.getString(TAG_CHANGE_LOG_TBL_NAME);
			if (object.has(TAG_CHANGE_LOG_TBL_PK))
				dto.changeLogTblPk = object.getString(TAG_CHANGE_LOG_TBL_PK);
			if (object.has(TAG_CHANGE_LOG_TBL_ACTION))
				dto.changeLogTblAction = object
						.getString(TAG_CHANGE_LOG_TBL_ACTION);
			if (object.has(TAG_CHANGE_LOG_TBL_CONTENT))
				dto.changeLogTblContent = object
						.getString(TAG_CHANGE_LOG_TBL_CONTENT);
			if (object.has(TAG_ADDED_DATE))
				dto.addedTime = object.getString(TAG_ADDED_DATE);

			arrChangeLog.add(dto);
		}

		return arrChangeLog;
	}

	public AboutsDTO parseAboutFromJson(String json, String id)
			throws JSONException {
		AboutsDTO about = new AboutsDTO();

		JSONObject object = new JSONObject(json);
		about.set_id(id);
		if (object.has(AboutsDTO.ADDED_DATE))
			about.setAddedDate(object.getString(AboutsDTO.ADDED_DATE));
		if (object.has(AboutsDTO.ABOUT_US_CATEGORY_DESCRIPTION_AR))
			about.setCategoryDescriptioAr(object
					.getString(AboutsDTO.ABOUT_US_CATEGORY_DESCRIPTION_AR));
		if (object.has(AboutsDTO.ABOUT_US_CATEGORY_DESCRIPTION))
			about.setCategoryDescription(object
					.getString(AboutsDTO.ABOUT_US_CATEGORY_DESCRIPTION));
		// TODO:
		if (object.has(AboutsDTO.ABOUT_US_CATEGORY_IMAGE))
			about.setAbout_us_category_image(object
					.getString(AboutsDTO.ABOUT_US_CATEGORY_IMAGE));
		if (object.has(AboutsDTO.ABOUT_US_CATEGORY_NUMBER))
			about.setCategoryNumber(object
					.getString(AboutsDTO.ABOUT_US_CATEGORY_NUMBER));
		if (object.has(AboutsDTO.ABOUT_US_CATEGORY_TITLE))
			about.setCategoryTitle(object
					.getString(AboutsDTO.ABOUT_US_CATEGORY_TITLE));
		if (object.has(AboutsDTO.ABOUT_US_CATEGORY_TITLE_AR))
			about.setCategoryTitleAr(object
					.getString(AboutsDTO.ABOUT_US_CATEGORY_TITLE_AR));

		return about;
	}

	public EventsDTO parseEventsFromJson(String json, String id)
			throws JSONException {
		EventsDTO events = new EventsDTO();

		JSONObject object = new JSONObject(json);
		events.set_id(id);
		// TODO:
		// events.setEventBitmapImage(null);
		if (object.has(EventsDTO.ADDED_DATE))
			events.setEventDate(object.getString(EventsDTO.ADDED_DATE));
		if (object.has(EventsDTO.EVENT_DESCRIPTION))
			events.setEventDescription(object
					.getString(EventsDTO.EVENT_DESCRIPTION));
		if (object.has(EventsDTO.EVENT_DESCRIPTION_AR))
			events.setEventDescriptionAr(object
					.getString(EventsDTO.EVENT_DESCRIPTION_AR));
		if (object.has(EventsDTO.EVENT_IMAGE))
			events.setEventImage(object.getString(EventsDTO.EVENT_IMAGE));
		if (object.has(EventsDTO.EVENT_TITLE))
			events.setEventTitle(object.getString(EventsDTO.EVENT_TITLE));
		if (object.has(EventsDTO.EVENT_TITLE_AR))
			events.setEventTitleAr(object.getString(EventsDTO.EVENT_TITLE_AR));
		if (object.has(EventsDTO.PRIORITY))
			events.setPriority(object.getString(EventsDTO.PRIORITY));

		return events;
	}

	public FitnessGroupDTO parseFitnessGroupFromJson(String json, String id)
			throws JSONException {
		FitnessGroupDTO fit = new FitnessGroupDTO();

		JSONObject object = new JSONObject(json);
		fit.setFitnessGroupId(id);
		if (object.has(FitnessGroupDTO.ADDED_DATE))
			fit.setFitnessGroupAddedDate(object
					.getString(FitnessGroupDTO.ADDED_DATE));
		if (object.has(FitnessGroupDTO.FITNESS_GROUP_NAME))
			fit.setFitnessGroupName(object
					.getString(FitnessGroupDTO.FITNESS_GROUP_NAME));
		if (object.has(FitnessGroupDTO.FITNESS_GROUP_NAME_AR))
			fit.setFitnessGroupNameAr(object
					.getString(FitnessGroupDTO.FITNESS_GROUP_NAME_AR));

		return fit;
	}

	public FitnessClassDTO parseFitnessClassFromJson(String json, String id)
			throws JSONException {
		FitnessClassDTO fit = new FitnessClassDTO();

		JSONObject object = new JSONObject(json);
		fit.setFitnessClassId(id);
		if (object.has(FitnessClassDTO.FITNESS_CLASS_DESCRIPTION))
			fit.setFitnessClassDescription(object
					.getString(FitnessClassDTO.FITNESS_CLASS_DESCRIPTION));
		if (object.has(FitnessClassDTO.FITNESS_CLASS_DESCRIPTION_AR))
			fit.setFitnessClassDescriptionAr(object
					.getString(FitnessClassDTO.FITNESS_CLASS_DESCRIPTION_AR));
		if (object.has(FitnessClassDTO.FITNESS_CLASS_ID))
			fit.setFitnessClassId(object
					.getString(FitnessClassDTO.FITNESS_CLASS_ID));
		if (object.has(FitnessClassDTO.FITNESS_CLASS_IMAGE))
			fit.setFitnessClassImage(object
					.getString(FitnessClassDTO.FITNESS_CLASS_IMAGE));
		if (object.has(FitnessClassDTO.FITNESS_CLASS_NAME))
			fit.setFitnessClassName(object
					.getString(FitnessClassDTO.FITNESS_CLASS_NAME));
		if (object.has(FitnessClassDTO.FITNESS_CLASS_NAME_AR))
			fit.setFitnessClassNameAr(object
					.getString(FitnessClassDTO.FITNESS_CLASS_NAME_AR));
		if (object.has(FitnessClassDTO.PRIORITY))
			fit.setPriority(object.getString(FitnessClassDTO.PRIORITY));
		if (object.has(FitnessClassDTO.FITNESS_CLASS_GROUP_ID))
			fit.setFitnessClassGroupId(object.getString(FitnessClassDTO.FITNESS_CLASS_GROUP_ID));
		return fit;
	}

	public FitnessScheduleDTO parseFitnessScheduleFromJson(String json,
			String id) throws JSONException {
		FitnessScheduleDTO fit = new FitnessScheduleDTO();

		JSONObject object = new JSONObject(json);
		fit.setFitnessScheduleId(id);
		if (object.has(FitnessScheduleDTO.FITNESS_SCHEDULE_CLASS_ID))
			fit.setFitnessScheduleClassId(object
					.getString(FitnessScheduleDTO.FITNESS_SCHEDULE_CLASS_ID));
		if (object.has(FitnessScheduleDTO.FITNESS_SCHEDULE_DAY))
			fit.setFitnessScheduleDay(object
					.getString(FitnessScheduleDTO.FITNESS_SCHEDULE_DAY));
		if (object.has(FitnessScheduleDTO.FITNESS_SCHEDULE_END_TIME))
			fit.setFitnessScheduleEndTime(object
					.getString(FitnessScheduleDTO.FITNESS_SCHEDULE_END_TIME));
		if (object.has(FitnessScheduleDTO.FITNESS_SCHEDULE_INSTRUCTOR_ID))
			fit.setFitnessScheduleInstructorId(object
					.getString(FitnessScheduleDTO.FITNESS_SCHEDULE_INSTRUCTOR_ID));
		if (object.has(FitnessScheduleDTO.FITNESS_SCHEDULE_START_DATE))
			fit.setFitnessScheduleStartDate(object
					.getString(FitnessScheduleDTO.FITNESS_SCHEDULE_START_DATE));
		if (object.has(FitnessScheduleDTO.FITNESS_SCHEDULE_START_TIME))
			fit.setFitnessScheduleStartTime(object
					.getString(FitnessScheduleDTO.FITNESS_SCHEDULE_START_TIME));

		return fit;
	}

	public InstructorDTO parseInstructorFromJson(String json, String id)
			throws JSONException {
		InstructorDTO in = new InstructorDTO();

		JSONObject object = new JSONObject(json);
		in.setInstrutorId(id);
		if (object.has(InstructorDTO.INSTRUCTOR_NAME))
			in.setInstructorName(object
					.getString(InstructorDTO.INSTRUCTOR_NAME));
		if (object.has(InstructorDTO.INSTRUCTOR_NAME_AR))
			in.setInstructorNameAr(object
					.getString(InstructorDTO.INSTRUCTOR_NAME_AR));

		return in;
	}

	public MerchantDTO parseMerchantFromJson(String json, String id)
			throws JSONException {
		MerchantDTO m = new MerchantDTO();

		JSONObject object = new JSONObject(json);
		m.setMerchantId(id);
		if (object.has(MerchantDTO.MERCHANT_IMAGE))
			m.setMerchantImage(object.getString(MerchantDTO.MERCHANT_IMAGE));
		if (object.has(MerchantDTO.MERCHANT_NAME))
			m.setMerchantName(object.getString(MerchantDTO.MERCHANT_NAME));
		if (object.has(MerchantDTO.PRIORITY))
			m.setPriority(object.getString(MerchantDTO.PRIORITY));

		return m;
	}

	public NewsModuleDTO parseNewsModuleFromJson(String json, String id)
			throws JSONException {
		NewsModuleDTO news = new NewsModuleDTO();

		JSONObject object = new JSONObject(json);
		news.setNewsModuleId(id);
		if (object.has(NewsModuleDTO.NEWS_MODULE_DATE))
			news.setNewsAddedDate(object
					.getString(NewsModuleDTO.NEWS_MODULE_DATE));
		if (object.has(NewsModuleDTO.NEWS_MODULE_DESCRIPTOIN))
			news.setNewsModuleDescription(object
					.getString(NewsModuleDTO.NEWS_MODULE_DESCRIPTOIN));
		if (object.has(NewsModuleDTO.NEWS_MODULE_DESCRIPTION_AR))
			news.setNewsModuleDescriptionAr(object
					.getString(NewsModuleDTO.NEWS_MODULE_DESCRIPTION_AR));
		if (object.has(NewsModuleDTO.NEWS_MODULE_IMAGE))
			news.setNewsModuleImage(object
					.getString(NewsModuleDTO.NEWS_MODULE_IMAGE));
		if (object.has(NewsModuleDTO.NEWS_MODULE_TITLE))
			news.setNewsModuleTitle(object
					.getString(NewsModuleDTO.NEWS_MODULE_TITLE));
		if (object.has(NewsModuleDTO.NEWS_MODULE_TITLE_AR))
			news.setNewsModuleTitleAr(object
					.getString(NewsModuleDTO.NEWS_MODULE_TITLE_AR));
		if (object.has(NewsModuleDTO.NEWS_MODULE_PRIORITY))
			news.setNewsPriority(object
					.getString(NewsModuleDTO.NEWS_MODULE_PRIORITY));

		return news;
	}

	public RetailersDTO parseRetailerFromJson(String json, String id)
			throws JSONException {
		RetailersDTO re = new RetailersDTO();

		JSONObject object = new JSONObject(json);
		re.setRetailerId(id);
		if (object.has(RetailersDTO.RETAILER_DATE))
			re.setRetailerAddedDate(object
					.getString(RetailersDTO.RETAILER_DATE));
		if (object.has(RetailersDTO.RETAILER_DESCRIPTION))
			re.setRetailerDescription(object
					.getString(RetailersDTO.RETAILER_DESCRIPTION));
		if (object.has(RetailersDTO.RETAILER_DESCRIPTION_AR))
			re.setRetailerDescriptionAr(object
					.getString(RetailersDTO.RETAILER_DESCRIPTION_AR));
		if (object.has(RetailersDTO.RETAILER_IMAGE))
			re.setRetailerImage(object.getString(RetailersDTO.RETAILER_IMAGE));
		if (object.has(RetailersDTO.RETAILER_NAME))
			re.setRetailerName(object.getString(RetailersDTO.RETAILER_NAME));
		if (object.has(RetailersDTO.RETAILER_NAME_AR))
			re.setRetailerNameAr(object
					.getString(RetailersDTO.RETAILER_NAME_AR));
		if (object.has(RetailersDTO.PRIORITY))
			re.setRetailerPriority(object.getString(RetailersDTO.PRIORITY));

		return re;
	}

	public SpaChildDTO parseSpaChildFromJson(String json, String id)
			throws JSONException {
		SpaChildDTO s = new SpaChildDTO();

		JSONObject object = new JSONObject(json);
		s.setSecId(id);
		if (object.has(SpaChildDTO.SPA_CATEGORY_ID))
			s.setSpaCatId(object.getString(SpaChildDTO.SPA_CATEGORY_ID));
		if (object.has(SpaChildDTO.SPA_SECTION_DESCRIPTION))
			s.setSpaDesc(object.getString(SpaChildDTO.SPA_SECTION_DESCRIPTION));
		if (object.has(SpaChildDTO.SPA_SECTION_DESCRIPTION_AR))
			s.setSpaDescAr(object
					.getString(SpaChildDTO.SPA_SECTION_DESCRIPTION_AR));
		if (object.has(SpaChildDTO.SPA_SECTION_PACKAGE_PRICE))
			s.setSpaPackagePrice(object
					.getString(SpaChildDTO.SPA_SECTION_PACKAGE_PRICE));
		if (object.has(SpaChildDTO.SPA_SECTION_TIME_DURATION))
			s.setSpaTimeDuration(object
					.getString(SpaChildDTO.SPA_SECTION_TIME_DURATION));
		if (object.has(SpaChildDTO.SPA_SECTION_TITLE))
			s.setSecTitle(object.getString(SpaChildDTO.SPA_SECTION_TITLE));
		if (object.has(SpaChildDTO.SPA_SECTION_TITLE_AR))
			s.setSecTitleAr(object.getString(SpaChildDTO.SPA_SECTION_TITLE_AR));
		if (object.has(SpaChildDTO.SPA_ADDED_DATE))
			s.setAddedDate(object.getString(SpaChildDTO.SPA_ADDED_DATE));

		return s;
	}

	public SpaParentDTO parseSpaParentFromJson(String json, String id)
			throws JSONException {
		SpaParentDTO s = new SpaParentDTO();
		JSONObject object = new JSONObject(json);
		s.set_id(id);
		if (object.has(SpaParentDTO.PRIORITY))
			s.setPriority(object.getString(SpaParentDTO.PRIORITY));
		if (object.has(SpaParentDTO.SPA_CATEGORY_TITLE))
			s.setSpaTitle(object.getString(SpaParentDTO.SPA_CATEGORY_TITLE));
		if (object.has(SpaParentDTO.SPA_CATEGORY_TITLE_AR))
			s.setSpaTitleAr(object
					.getString(SpaParentDTO.SPA_CATEGORY_TITLE_AR));

		return s;
	}

}
